﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Odyssey.Controls;
using Odyssey.Controls.Classes;
using System.Windows.Controls.Primitives;
using WpfELearningAuthoringTool;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using ScormModel;
using ScormModel.View;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ScormModel.HtmlBuilder;
using System.Collections.ObjectModel;
using ScormModel.Question;
namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for ExamDialog.xaml
    /// </summary>
    public partial class ExamDialog : Window
    {
        RibbonBarRegion.RibbonBarHeader parentChild;
        ObservableCollection<QuestionTest> listQuestions = new ObservableCollection<QuestionTest>();
        public ExamDialog()
        {
            InitializeComponent();
            listPossibleAnwser.ItemsSource = listQuestions;  
        }
        string currentDirectory;
        WebBrowser browser;
        ImsManifest imsmanifest;
        public string CurrentDirectory
        {
            get { return this.currentDirectory; }
            set { this.currentDirectory = value; }
        }
        public ImsManifest Imsmanifest
        {
            get { return this.imsmanifest; }
            set { this.imsmanifest = value; }
        }
        public WebBrowser WebBrowser
        {
            get { return this.browser; }
            set { this.browser = value; }
        }
        private string findTargetObj(string moduleName)
        {
            string obj = "";
            for (int i = 0; i < imsmanifest.ListObjectiveLog.Count; i++)
            {
                if (imsmanifest.ListObjectiveLog[i].ModuleName == moduleName)
                    return imsmanifest.ListObjectiveLog[i].ObjectiveId;
                else
                    continue;
            }
            return obj;
        }
        public RibbonBarRegion.RibbonBarHeader ParentChild
        {
            get { return this.parentChild; }
            set { this.parentChild = value; }
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            QuestionTypeDialog examDialog = new QuestionTypeDialog();
            examDialog.WebBrowser = browser;
            examDialog.Owner = this;
            examDialog.ParentChild = parentChild;
            examDialog.CurrentDirectory = currentDirectory;
            examDialog.Imsmanifest = imsmanifest;
            examDialog.ListQuestions = listQuestions;
            examDialog.IsAddNew = false;
            examDialog.ShowDialog();
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            listQuestions.RemoveAt(listPossibleAnwser.SelectedIndex);
            List<QuestionTest> law = new List<QuestionTest>();
            for (int i = 0; i < listQuestions.Count; i++)
            {
                QuestionTest qi = listQuestions[i];
                qi.Id = (i + 1).ToString();
                law.Add(qi);
            }
            listQuestions.Clear();
            for (int i = 0; i < law.Count; i++)
            {
                listQuestions.Add(law[i]);
            }
        }
        private void ViewDetail_Click(object sender, RoutedEventArgs e)
        {
                if (listQuestions[listPossibleAnwser.SelectedIndex].QuestionType == "single")
                {
                    SimpleChoiceQuestionDialog scq = new SimpleChoiceQuestionDialog();
                    scq.ParentChild = parentChild;
                    scq.Owner = parentChild;
                    scq.CurrentDirectory = currentDirectory;
                    scq.WebBrowser = browser;
                    scq.Imsmanifest = imsmanifest;
                    scq.IsAddNew = false;
                    scq.QuestionTest = listQuestions[listPossibleAnwser.SelectedIndex];
                    scq.ShowDialog();
                }
                else if (listQuestions[listPossibleAnwser.SelectedIndex].QuestionType == "true_false")
                {
                    TrueFalseQuestionDialog tfq = new TrueFalseQuestionDialog();
                    tfq.ParentCHild = parentChild;
                    tfq.Owner = parentChild;
                    tfq.CurrentDirectory = currentDirectory;
                    tfq.WebBrowser = browser;
                    tfq.Imsmanifest = imsmanifest;
                    tfq.IsAddNew = false;
                    tfq.QuestionTest = listQuestions[listPossibleAnwser.SelectedIndex];
                    tfq.ShowDialog();
                }
                else if (listQuestions[listPossibleAnwser.SelectedIndex].QuestionType == "multiple")
                {
                    MultipleChoiceQuestionDialog mcq = new MultipleChoiceQuestionDialog();
                    mcq.ParentChild = parentChild;
                    mcq.Owner = parentChild;
                    mcq.CurrentDirectory = currentDirectory;
                    mcq.WebBrowser = browser;
                    mcq.Imsmanifest = imsmanifest;
                    mcq.IsAddNew = false;
                    mcq.QuestionTest = listQuestions[listPossibleAnwser.SelectedIndex];
                    mcq.ShowDialog();
                }
                else if (listQuestions[listPossibleAnwser.SelectedIndex].QuestionType == "order")
                {
                    OrderChoiceQuestionDialog ocq = new OrderChoiceQuestionDialog();
                    ocq.ParentChild = parentChild;
                    ocq.Owner = parentChild;
                    ocq.CurrentDirectory = currentDirectory;
                    ocq.WebBrowser = browser;
                    ocq.Imsmanifest = imsmanifest;
                    ocq.IsAddNew = false;
                    ocq.QuestionTest = listQuestions[listPossibleAnwser.SelectedIndex];
                    ocq.ShowDialog();
                }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try { Convert.ToInt32(numCorrectAnswer.Text); }
            catch {
                MessageBox.Show("Invalid number");
                return;
            }
            if (listQuestions.Count < Convert.ToInt32(numCorrectAnswer.Text))
            {
                MessageBox.Show("Num of Correct Answer is invalid");
                return;
            }
            QuestionTestData qtd = new QuestionTestData();
            qtd.ListQuestions = listQuestions;
            qtd.NumOfCorrectAnswer = numCorrectAnswer.Text;
            if (cbSetObj.IsChecked == true) {
                qtd.TargetObj = findTargetObj(moduleCollection.SelectedValue.ToString());
            }
            HtmlDocument content= qtd.createTest();
            browser.NavigateToString(content.HTML);
            Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnHelp_Click(object sender, RoutedEventArgs e)
        {

        }

        private void moduleCollection_Checked(object sender, RoutedEventArgs e)
        {
            
            moduleCollection.Items.Clear();
            ManifestProcessing mp = new ManifestProcessing(imsmanifest);
            mp.BuildModuleObjecttiveHashTable();
            if (imsmanifest.ListObjectiveLog.Count > 0)
            {
                for (int i = 0; i < imsmanifest.ListObjectiveLog.Count; i++)
                {
                    ComboBoxItem ci = new ComboBoxItem();
                    ci.Content = imsmanifest.ListObjectiveLog[i].ModuleName;
                    moduleCollection.Items.Add(ci);
                }
                moduleCollection.IsEnabled = true;
                moduleCollection.SelectedIndex = 0;
            }
            else {
                moduleCollection.IsEnabled = false;
                cbSetObj.IsChecked = false;
                MessageBox.Show("Can't set objective because of no module");
                return;
            }
        }

        private void moduleCollection_UnChecked(object sender, RoutedEventArgs e)
        {
            moduleCollection.IsEnabled = false;
            moduleCollection.Items.Clear();
        }
    }
}
